package com.wxzz.elearing.course.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wxzz.elearing.course.entity.Curriculum;
import com.wxzz.elearing.course.entity.StudyPlanUnit;
import com.wxzz.elearing.course.vo.LastSeeVo;
import jdk.nashorn.internal.runtime.options.Option;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Optional;

public interface StudyPlanUnitMapper extends BaseMapper<StudyPlanUnit> {

    /**
     * 列出该学习计划下的课程
     *
     * @return
     */
    @Select("SELECT\n" +
            "\tc.*\n" +
            "FROM\n" +
            "\tstudy_plan sp\n" +
            "\tJOIN study_plan_unit spu ON sp.id = spu.study_plan_id\n" +
            "\tJOIN study_plan_curriculum spc ON spu.id = spc.study_plan_unit_id \n" +
            "\tJOIN curriculum c ON spc.curriculum_id = c.id\n" +
            "WHERE\n" +
            "\tsp.id = #{studyPlanId} AND sp.deleted = 0 AND spu.deleted = 0 AND spc.deleted = 0 AND c.deleted = 0")
    List<Curriculum> listStudyPlanUnit(@Param("studyPlanId") Long studyPlanId);


    /**
     * 根据课程id查询单元名称和计划id
     * @param curriculumId
     * @return
     */
    @Select("SELECT\n" +
            "\tsp.id ,spu.unit_name\n" +
            "FROM\n" +
            "\tstudy_plan sp\n" +
            "\tJOIN study_plan_unit spu ON sp.id = spu.study_plan_id\n" +
            "\tJOIN study_plan_curriculum spc ON spu.id = spc.study_plan_unit_id\n" +
            "WHERE\n" +
            "\tspc.curriculum_id = #{curriculumId} AND sp.deleted = 0 AND spu.deleted = 0 AND spc.deleted = 0")
    List<LastSeeVo> selectUnitByCurriculumId(@Param("curriculumId") Long curriculumId);

    /**
     * 根据学习计划id查询学习单元
     * @param studyPlanId
     * @return
     */
    @Select("SELECT * FROM `study_plan_unit` WHERE study_plan_id = #{studyPlanId} AND deleted = 0")
    List<StudyPlanUnit> listStudyPlanUnitByStudyPlanId(@Param("studyPlanId") Long studyPlanId);

    /**
     * 根据学习计划单元查询出对应的课程
     * @param studyPlanUnitId
     * @return
     */
    @Select("SELECT\n" +
            "\tc.*\n" +
            "FROM\n" +
            "\tstudy_plan_curriculum spc\n" +
            "\tJOIN curriculum c ON spc.curriculum_id = c.id \n" +
            "WHERE\n" +
            "\tspc.study_plan_unit_id = #{studyPlanUnitId} \n" +
            "\tAND c.deleted = 0 \n" +
            "\tAND spc.deleted = 0")
    List<Curriculum> selectCurriculumByStudyPlanUnitId(@Param("studyPlanUnitId") Long studyPlanUnitId);
}
